import { getOrderMap, getOrderInfo } from '@/services/api';
import { queryUserinfo } from '@/services/user';
export default {
  namespace: 'profile',

  state: {
    userInfo: {},
    contractOrder: [],
    orderInfo: {},
    consignee: {},
  },

  effects: {
    *fetchUserInfo({ payload }, { call, put }) {
      const response = yield call(queryUserinfo, payload);
      const orderData = yield call(getOrderMap, response.data.openid);
      yield put({
        type: 'saveUserinfo',
        payload: response.data,
      });
      yield put({
        type: 'saveOrderData',
        payload: orderData.data,
      });
    },
    *fetchOrderInfo({ payload }, { call, put }) {
      const response = yield call(getOrderInfo, payload);
      yield put({
        type: 'saveOrderInfo',
        payload: response.data,
      });
      yield put({
        type: 'saveConsigneeInfo',
        payload: JSON.parse(response.data.address),
      });
    },
  },

  reducers: {
    show(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    saveOrderData(state, action) {
      return {
        ...state,
        contractOrder: action.payload,
      };
    },
    saveOrderInfo(state, action) {
      return {
        ...state,
        orderInfo: action.payload,
      };
    },
    saveConsigneeInfo(state, action) {
      return {
        ...state,
        consignee: action.payload,
      };
    },
    saveUserinfo(state, action) {
      return {
        ...state,
        userInfo: action.payload,
      };
    },
  },
};
